<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="资源任务单号：" prop="iNumber">
        <el-input
          v-model="queryParams.iNumber"
          placeholder="请输入资源任务单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源任务类型：" prop="iType">
      <el-radio-group v-model="queryParams.iType" >
     <el-radio :label='0' @click.native.prevent="changeRadio(0)">资源提供单</el-radio>
     <el-radio :label='1' @click.native.prevent="changeRadio(1)">资源需求单</el-radio>
      </el-radio-group>
      </el-form-item>
      <el-form-item label="企业名称：" prop="iEntName">
        <el-input
          v-model="queryParams.iEntName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
            <el-button type="primary" round  @click="openDialog">资源任务单排序详情</el-button>
    </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          
          @click="handleUpdate"
          v-hasPermi="['system:task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          
          @click="handleDelete"
          v-hasPermi="['system:task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

   <el-row>
    <el-col
    v-for = "item in taskList"
    :key="item.id"  
    :span="3"
    style="margin-bottom: 5px"
    :offset="2"
    >
    <el-card>
      <div slot="header" class="clearfix" >
        <i class="el-icon-s-data"/><span style="margin-left: 5px">{{typef(item.iType)}}</span>
        <div 
        style="display: inline-block;float:right;cursor: pointer"
        @click="doEdit(item.id)">
        <el-tooltip effect="dark" content="查看详情" placement="top">
          <i class="el-icon-edit-outline" style="margin-left: 15px" />
        </el-tooltip>
        </div>
      </div>
      <div>
        <ul class="role-info">
          <li>
            <div class="role-left">资源任务单号：{{item. iNumber}}</div>
            </li>
            <li>
            <div class="role-left">企业名称：{{item. iEntName}}</div>
            </li>
            <li>
            <div class="role-left">企业管理资源类型：{{item. iResType}}</div>
            </li>
            </ul>
            </div>
    </el-card>
  </el-col>
   </el-row>
  <el-row>
  <el-col :span="12" class="card-box">
         <el-card>
           <div slot="header"><span><i class="el-icon-pie-chart"></i> 资源任务单比例</span></div>
           <div class="el-table el-table--enable-row-hover el-table--medium">
            <div>
             <div ref="chart" style="width: 600px; height: 400px;"></div>
            </div>
             <div ref="commandstats" style="height: 420px" />
           </div>
         </el-card>
       </el-col>
       <el-col :span="12" class="card-box">
         <el-card>
           <div slot="header"><span><i class="el-icon-odometer"></i> 企业的资源任务单数量排行</span></div>
           <div class="el-table el-table--enable-row-hover el-table--medium">
            <div>
              <div  ref="chart1" style="width: 600px; height: 400px;"></div>
            </div>
             <div ref="usedmemory" style="height: 420px" ></div>
           </div>
         </el-card>
       </el-col>
     </el-row>
    <!-- <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资源信息任务单号" align="center" prop="iNumber" />
      <el-table-column label="资源信息任务类型" align="center"  :formatter="typeFormat" prop="iType"/>
      <el-table-column label="企业名称" align="center" prop="iEntName" />
      <el-table-column label="企业管理资源类型" align="center" prop="iResType" />
      <el-table-column label="企业管理资源数量" align="center" prop="iResNum" />
      <el-table-column label="企业的最低价" align="center"  prop="iPriLow" />
      <el-table-column label="企业的最高价" align="center"  prop="iPriHigh" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:task:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table> -->
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资源信息任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="资源信息任务单号" prop="iNumber">
          <el-input v-model="form.iNumber" placeholder="请输入资源信息任务单号" />
        </el-form-item>
        <el-form-item label="资源信息任务类型" prop="iType">
        <el-radio v-model="form.iType" :label='0'>资源提供单</el-radio>
        <el-radio v-model="form.iType" :label='1'>资源需求单</el-radio>
       </el-form-item>
        <el-form-item label="企业名称" prop="iEntName">
          <el-input v-model="form.iEntName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业管理资源类型" prop="iResType">
          <el-input v-model="form.iResType" placeholder="企业管理资源类型" />
        </el-form-item>
        <el-form-item label="企业管理资源数量" prop="iResNum">
          <el-input v-model="form.iResNum" placeholder="请输入企业管理资源数量" />
        </el-form-item>
        <el-form-item label="企业的最低价" prop="iPriLow">
          <el-input v-model="form.iPriLow" placeholder="请输入企业的最低价" />
        </el-form-item>
        <el-form-item label="企业的最高价" prop="iPriHigh">
          <el-input v-model="form.iPriHigh" placeholder="请输入企业的最高价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible="showDialog" @close="closeDialog" title="资源调度单排序详情">
    <el-button type="primary" round  @click="openDialog1">预测后的调度单排序</el-button>
    <div class="ranking-table">
    <el-table :data="data1" stripe class="table-dark table-striped">
      <el-table-column prop="rank" label="Rank">
        <template v-slot="scope">
          <span>{{ scope.row.rank }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="资源调度号">
        <template v-slot="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="company" label="该调度单企业">
        <template v-slot="scope">
          <span>{{ scope.row.company }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="time" label="资源调度单创建时间">
        <template v-slot="scope">
          <span>{{ scope.row.time }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="排序更改情况">
        <template v-slot="scope">
          <span :class="[scope.row.rankChange > 0 ? 'arrow-up' : 'arrow-down']">
            {{ scope.row.rankChange >= 0 ? '▲' : '▼' }}
          </span>
          {{ scope.row.change }}
        </template>
      </el-table-column> -->
    </el-table>
  </div>    
    </el-dialog>

    <el-dialog :visible="showDialog1" @close="closeDialog1" title="预测后的任务单排序详情">
    <div class="ranking-table">
      <el-table :data="data2" stripe class="table-dark table-striped">
      <el-table-column prop="rank" label="Rank">
        <template v-slot="scope">
          <span>{{ scope.row.rank }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="资源调度号">
        <template v-slot="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="company" label="该调度单企业">
        <template v-slot="scope">
          <span>{{ scope.row.company }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="time" label="资源调度单创建时间">
        <template v-slot="scope">
          <span>{{ scope.row.time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序更改情况">
        <template v-slot="scope">
          <span :class="[scope.row.rankChange > 0 ? 'arrow-up' : 'arrow-down']">
            {{ scope.row.rankChange >= 0 ? '▲' : '▼' }}
          </span>
          {{ scope.row.change }}
        </template>
      </el-table-column>
    </el-table>
  </div>    
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/Resource/task";
import * as echarts from 'echarts';

export default {
  
  name: "Task",
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showDialog: false,
      showDialog1: false,
      // 总条数
      total: 0,
      // 资源信息任务表格数据
      taskList: [],
      //task
      tasknum:0,
      taskarr:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        iType: null,
        iNumber: null,
        iEntName: null,
        iResType: null,
        iResNum: null,
        iPriLow: null,
        iPriHigh: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      data1: [
        { rank:1,id: 'A11', company: '宁德时代', time: '05-10-16:04:23', change: 0, rankChange: 1 },
        { rank:2,id: 'B1',company: '潍柴动力',  time: '05-10-16:05:21', change: 0, rankChange: 1 },
        { rank:3,id: 'A2', company: '宁德时代', time: '05-10-16:04:11', change: 0, rankChange: 1 },
        { rank:4,id: 'B33',company: '比亚迪',  time: '05-10-16:04:09', change: 0, rankChange: 1 },
        { rank:5,id: 'A22', company: '比亚迪', time: '05-10-16:04:57', change: 0, rankChange: 1 },
        { rank:6,id: 'A33', company: '理想汽车', time: '05-10-16:04:33', change: 0, rankChange: 1 },
        { rank:7,id: 'A34', company: '小鸭家电', time: '05-10-16:08:32', change: 0, rankChange: 1 },
        { rank:8,id: 'A16', company: '宁德时代', time: '05-10-16:10:02', change: 0, rankChange: 1 },
      ],
      data2: [
        { rank:1,id: 'A11', company: '宁德时代', time: '05-10-16:04:23', change: 0, rankChange: 1 },
        { rank:2,id: 'A2', company: '宁德时代', time: '05-10-16:04:11', change: 1, rankChange: 1 },
        { rank:3,id: 'B1',company: '潍柴动力',  time: '05-10-16:05:21', change: -1, rankChange: -1 },
        { rank:4,id: 'A33', company: '理想汽车', time: '05-10-16:04:33', change: 2, rankChange: 1},
        { rank:5,id: 'A22', company: '比亚迪', time: '05-10-16:04:57', change: 0, rankChange: 1 },
        { rank:6,id: 'A16', company: '宁德时代', time: '05-10-16:10:02', change: 2, rankChange: 1},
        { rank:7,id: 'B33',company: '比亚迪',  time: '05-10-16:04:09', change: -4, rankChange: -1 },
        { rank:8,id: 'A34', company: '小鸭家电', time: '05-10-16:08:32', change: -1, rankChange: -1 },
      ]
    };
  },
  created() {
    this.getList();
    this.taskInfo();
  },
  mounted() {
    this.getData();

  },

  methods: {
  
    /** redio取消选中*/
   changeRadio(e){
    e===this.queryParams.iType?this.queryParams.iType=null:this.queryParams.iType=e
   },
    /** 任务类型转换 */
  
    typeFormat(row){
      if(row.iType=='0')return '资源提供单'
      else  return '资源需求单'
      },

    typef(type){
      if(type=='0')return '资源提供单'
      else  return '资源需求单'  
    },
    /** 查询资源信息任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iId: null,
        iType: null,
        iNumber: null,
        iEntName: null,
        iResType: null,
        iResNum: null,
        iPriLow: null,
        iPriHigh: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.iId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资源信息任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iId = row.iId || this.ids
      getTask(iId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资源信息任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.iId != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getData();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getData();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const iIds = row.iId || this.ids;
      this.$modal.confirm('是否确认删除资源信息任务编号为"' + iIds + '"的数据项？').then(function() {
        return delTask(iIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    },
    async taskInfo(){
      await listTask(this.queryParams).then(response => {
        
        for (var i=0;i<response.rows.length;i++){
          this.taskarr.push(response.rows[i]);
          this.tasknum++;
        }
      //  console.log(scaleData1);
      });
    },
    
    getData(){
      listTask(this.queryParams).then(response => {
        const data=response.rows;
        this.renderChart1(data);
        this.renderChart2(data);
      }).catch(error=>{console.log('Error'+error)});
    },

    renderChart1(data) {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom);

      var taskarr=data;
      let scaleLegend=['资源需求单','资源提供单'];
      var tasknum=taskarr.length;
      var tgd=0;
      for (var i=0;i<tasknum;i++){
        if (taskarr[i].iType=='0')
        tgd++;
      }
      var xqd=tasknum-tgd;
      var xqd=tasknum-tgd;
      const option = {
        title: {
          left: 'right',
      
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          data:scaleLegend,
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: [
            { value: tgd, name: '资源提供单',itemStyle: { color: '#FF0000' } },
              { value: xqd, name: '资源需求单',itemStyle: { color: '#FFA500' } }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0.5, 0.1, 0, 0.5)'
              }
            }
          }
        ]
      };
      option &&myChart.setOption(option);
      
  },

  renderChart2(data){
      const chartDom1 = this.$refs.chart1;
      const myChart1 = echarts.init(chartDom1);
      var taskarr=data;
      var taskmap=new Map();

      for (var i=0;i<taskarr.length;i++){
       if(taskmap.has(taskarr[i].iEntName)){
        var e=taskmap.get(taskarr[i].iEntName);
        taskmap.set(taskarr[i].iEntName,e+1);
       }else{
        taskmap.set(taskarr[i].iEntName,1);
       }
      }
      var arrayObj=Array.from(taskmap);
      arrayObj.sort(function(a,b){return a[1]-b[1]});
      taskmap = new Map(arrayObj.map(i => [i[0], i[1]]));
      var taskx=[];
      var tasky=[];
      taskmap.forEach((value, key)=>{
        taskx.push(key);
        tasky.push(value);  
        
      });
      
      const option = {
        xAxis: {
       type: 'category',
      data: taskx
      },
    yAxis: {
    type: 'value',
    splitNumber:4
     },
     series: [
    {
      data: tasky,
      type: 'bar',
      showBackground: true,
      backgroundStyle: {
        color: 'rgba(180, 180, 180, 0.2)'
      }
    }
  ]
      };

      option&&myChart1.setOption(option);
    },
    openDialog() {
      this.showDialog = true;
      // this.$nextTick(() => {
      //   // this.renderGraph();
      // });
    },
    closeDialog() {
      this.showDialog = false;
    },
    openDialog1() {
      this.showDialog1 = true;
      // this.$nextTick(() => {
      //   // this.renderGraph();
      // });
    },
    closeDialog1() {
      this.showDialog1 = false;
    }
}
};
</script>
<style scoped>
.ranking-table {
  width: 80%;
  margin: 20px auto;
  background-color: #f2f3f6;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

.arrow-up {
  color: #4caf50;
}
.arrow-down {
  color: #f44336;
}
</style>